package com.study.studyhome.m31;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

@Slf4j
public class RoleData {
    private float[] properties;

    public RoleData(float[] properties) {
        this.properties = properties;

        if (properties.length != 5) {
            throw new RuntimeException("角色需要有5个属性");
        }
    }

    /**
     * 策划配置每个属性换算战力的方式
     *
     * @return
     */
    public float getAbalityValue() {
        return (float) (properties[0] * 1 + properties[1] * 1.1 + properties[2] * 1.2 + properties[3] * 1.3 + properties[4] * 1.4);
    }

    @Override
    public String toString() {
        return "RoleData{" +
                "properties=" + Arrays.toString(properties) +
                '}';

    }

    public static RoleData find(List<RoleData> roleDataList, int inputRoleDataListIndex) {
        // 默认第一个是最小差距的
        RoleData minDiffRoleData = null;
        float minDiff = 0;

        float targetAbality = roleDataList.get(inputRoleDataListIndex).getAbalityValue();

        for (int i = 0; i < roleDataList.size(); i++) {
            // 不能和自己比较
            if (i == inputRoleDataListIndex) {
                continue;
            }
            if (minDiffRoleData == null) {
                minDiffRoleData = roleDataList.get(i);
                minDiff = Math.abs(targetAbality - minDiffRoleData.getAbalityValue());
            } else {
                RoleData curRoleData = roleDataList.get(i);
                float curDiff = Math.abs(targetAbality - curRoleData.getAbalityValue());
                if (curDiff < minDiff) {
                    minDiffRoleData = curRoleData;
                    minDiff = curDiff;
                }
            }
        }

        return minDiffRoleData;
    }

    public static void main(String[] args) {
        ArrayList<RoleData> roleDataList = new ArrayList<>(Arrays.asList(
                new RoleData(new float[]{1, 1, 1, 1, 1}),
                new RoleData(new float[]{2, 3, 4, 5, 6}),
                new RoleData(new float[]{10, 11, 11, 10, 12}),
                new RoleData(new float[]{16, 17, 18, 19, 19}),
                new RoleData(new float[]{7, 8, 9, 10, 11}),
                new RoleData(new float[]{10, 11, 11, 11, 12})
        ));
        int roleIndex=new Random().nextInt(roleDataList.size()-1);
        RoleData nearRoleData = find(roleDataList, roleIndex);
       log.info("对比的角色的战力:" + roleDataList.get(roleIndex).getAbalityValue());
       log.info("战力接近的角色:" + nearRoleData.getAbalityValue());
    }



}
